package com.spring.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.spring.gowhere.R;
import com.spring.httputils.RequestParams;
import com.spring.httputils.RestClient;
import com.spring.httputils.TextHttpResponseHandler;
import com.spring.tool.App;

import org.apache.http.Header;
import org.json.JSONObject;
import org.json.alipay.JSONException;

/**
 * 支付
 */
public class PayActivity extends BaseActivity {
    private RadioButton rb1, rb2;
    private TextView pice;
    private EditText pice_ediet;
    private Button pay;
    private RelativeLayout ZFB, VX;
    private String username, logintype;
    RestClient restClient = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        TextView textView = (TextView) findViewById(R.id.title_tv);
        textView.setText("自动充值");
        ImageButton imageButton = (ImageButton) findViewById(R.id.title_left_ib);
        imageButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        rb1 = (RadioButton) findViewById(R.id.rb1);
        rb2 = (RadioButton) findViewById(R.id.rb2);
        pice = (TextView) findViewById(R.id.text);
        pice_ediet = (EditText) findViewById(R.id.star_time);
        pay = (Button) findViewById(R.id.pay);
        VX = (RelativeLayout) findViewById(R.id.VX);
        ZFB = (RelativeLayout) findViewById(R.id.ZFB);
        SharedPreferences userInfo = this.getSharedPreferences(
                "user_info", 0);
        username = userInfo.getString("username", "");
        logintype = userInfo.getString("logintype", "");
        restClient = new RestClient(this);
        ZFB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rb1.setChecked(true);
                if (rb1.isChecked()) {
                    rb2.setChecked(false);
                } else {
                    rb2.setChecked(true);
                }

            }
        });
        VX.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                rb2.setChecked(true);
//                if (rb2.isChecked()) {
//                    rb1.setChecked(false);
//                } else {
//                    rb1.setChecked(true);
//                }
                rb2.setChecked(false);
                Toast.makeText(PayActivity.this, "暂时未开通", Toast.LENGTH_SHORT).show();
            }

        });
        pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(pice_ediet.getText().toString())) {
                    Toast.makeText(PayActivity.this, "请输入您要充值的金额", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (pice.getText().toString().equals("0")){
                    Toast.makeText(PayActivity.this, "你的金额不允许填0", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (rb1.isChecked()) {
                    //接口1
                    SendHttpPay sendhttp = new SendHttpPay(
                            App.getInstance().url
                                    + "/GetCustSn"
                    );
                    sendhttp.run();
                }

            }
        });
        pice_ediet.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(pice_ediet.getText().toString())) {
                    pice.setText("0");
                } else {
                    pice.setText(String.valueOf(Integer.parseInt(pice_ediet.getText().toString())));
                }

            }
        });


        rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                if (rb1.isChecked()) {
//                    rb2.setChecked(false);
//                } else {
//                    rb2.setChecked(true);
//                }

            }
        });
        rb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                if (rb2.isChecked()) {
//                    rb1.setChecked(false);
//                } else {
//                    rb1.setChecked(true);
//                }
                Toast.makeText(PayActivity.this, "暂时未开通", Toast.LENGTH_SHORT).show();
                rb2.setChecked(false);
//

            }
        });
    }

    class SendHttpPay implements Runnable {
        String url;

        SendHttpPay(String url) {
            this.url = url;
        }

        @Override
        public void run() {
            RequestParams requestparams = new RequestParams(url, "POST");
            requestparams.put("LoginAccount", username);
            requestparams.put("LoginType", logintype);
            requestparams.put("TransferMoney", pice.getText().toString());
            Log.i("TAG", "LoginAccount" + username);
            Log.i("TAG", "LoginType" + logintype);
            Log.i("TAG", "TransferMoney" + pice.getText().toString());
            TextHttpResponseHandler textHttpResponseHandler = new TextHttpResponseHandler() {

                @Override
                public void onSuccess(int statusCode, Header[] headers,
                                      String responseString) {
                    // TODO Auto-generated method stub
                    Log.e("TAG", "responseString1122" + responseString);
                    try {
                        Analysis(responseString);
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFailure(int statusCode, Header[] headers,
                                      String responseString, Throwable throwable) {
                    // TODO Auto-generated method stub
                    Log.i("TAG", "responseString22" + responseString);

                }
            };
            try {
                restClient.post(url, requestparams, textHttpResponseHandler);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        /**
         * json数据解析
         *
         * @param strResult
         * @throws JSONException
         */
        private void Analysis(String strResult) throws JSONException {
            /**
             * 这里需要分析服务器回传的json格式数据，
             */
            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(strResult);
            } catch (org.json.JSONException e) {
                e.printStackTrace();
            }
            try {
                String returnMsg = jsonObject.getString("returnMsg");
                if (returnMsg == null || "".equals(returnMsg)) {
                    // TODO Auto-generated method stub
                    String CustSn = jsonObject.getString("CustSn");
                    Log.i("TAG", "CustSn:" + CustSn);
                    Intent intent = new Intent(PayActivity.this, PayZFBActivity.class);
                    intent.putExtra("pice", pice.getText().toString());
                    intent.putExtra("CustSn", CustSn);
                    startActivity(intent);
                } else {
                    Toast.makeText(PayActivity.this, returnMsg,  Toast.LENGTH_SHORT).show();
                    return;
                }
            } catch (org.json.JSONException e) {
                e.printStackTrace();
            }
        }

    }
}

